package exam.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import exam.bean.UserInfo;
import exam.dao.UserInfoDao;
import exam.service.IUserInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service
public class UserInfoServiceImpl implements IUserInfoService {
    @Autowired
    UserInfoDao userInfoDao;

    @Autowired
    BCryptPasswordEncoder bCryptPasswordEncoder;      //加密

    @Override
    public int insertInfo(UserInfo userInfo) {
        //密码加密
        userInfo.setPassword(bCryptPasswordEncoder.encode(userInfo.getPassword()));
        return userInfoDao.insertInfo(userInfo);
    }

    @Override
    public int updateInfo(UserInfo userInfo) {
        return userInfoDao.updateInfo(userInfo);
    }

    @Override
    public int deleteInfo(int id) {
        return userInfoDao.deleteInfo(id);
    }

    @Override
    public PageInfo<UserInfo> selectInfo(int pageNum, int pageSize,Map<String,Object> queryMap) {

        PageHelper.startPage(pageNum,pageSize);

        return new PageInfo<>(userInfoDao.selectInfo(queryMap));
    }

    @Override
    public UserInfo selectInfoSingle(Map<String, Object> map) {
        List<UserInfo> re = userInfoDao.selectInfo(map);
        if(re == null || re.size() == 0){
            return null;
        }else{
            return re.get(0);
        }
    }
}
